package com.beemans.admob.ads;

import android.app.Activity;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.admanager.AdManagerAdRequest;
import com.google.android.gms.ads.admanager.AdManagerInterstitialAd;
import com.google.android.gms.ads.admanager.AdManagerInterstitialAdLoadCallback;

public class AbInsertAdWrapper {
    @NonNull
    private final String placementId;
    @Nullable
    private AdManagerInterstitialAd mInterstitialAd;

    private boolean isLoadingAd = false;
    private boolean isShowingAd = false;
    private boolean isAdShown = false;

    public AbInsertAdWrapper(@NonNull String placementId) {
        this.placementId = placementId;
    }

    public boolean isAdvertReady() {
        return mInterstitialAd != null && !isAdShown;
    }

    public boolean isAdvertLoading() {
        return isLoadingAd;
    }

    public boolean isAdvertShowing() {
        return isShowingAd;
    }

    public void loadAd(
            @NonNull Context context,
            @NonNull AbAdNetworkListener networkListener
    ) {
        isLoadingAd = true;

        try {
            final AdManagerAdRequest request = new AdManagerAdRequest.Builder().build();
            AdManagerInterstitialAd.load(context, placementId, request, new AdManagerInterstitialAdLoadCallback() {
                @Override
                public void onAdLoaded(@NonNull AdManagerInterstitialAd adManagerInterstitialAd) {
                    isLoadingAd = false;
                    mInterstitialAd = adManagerInterstitialAd;

                    networkListener.onAdLoaded();
                }

                @Override
                public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                    isLoadingAd = false;

                    networkListener.onAdFailedToLoad(loadAdError.toString());
                }
            });
        } catch (Throwable throwable) {
            isLoadingAd = false;
            networkListener.onAdFailedToLoad(throwable.getMessage());
        }
    }

    public void showAd(
            @NonNull Activity activity,
            @NonNull AbAdEventListener eventListener
    ) {
        if (mInterstitialAd == null) {
            eventListener.onAdDisplayFailed("");
            return;
        }

        final AdManagerInterstitialAd interstitialAd = mInterstitialAd;
        mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
            @Override
            public void onAdImpression() {
                eventListener.onAdImpression(interstitialAd.getResponseInfo());
            }

            @Override
            public void onAdClicked() {
                eventListener.onAdClicked(interstitialAd.getResponseInfo());
            }

            @Override
            public void onAdDismissedFullScreenContent() {
                isShowingAd = false;

                eventListener.onAdClosed(interstitialAd.getResponseInfo());
            }

            @Override
            public void onAdFailedToShowFullScreenContent(@NonNull AdError adError) {
                isShowingAd = false;

                eventListener.onAdDisplayFailed(adError.toString());
            }

            @Override
            public void onAdShowedFullScreenContent() {
            }
        });

        isAdShown = true;
        isShowingAd = true;

        try {
            mInterstitialAd.show(activity);
            mInterstitialAd = null;
        } catch (Throwable throwable) {
            isAdShown = false;
            isShowingAd = false;
            eventListener.onAdDisplayFailed(throwable.getMessage());
        }
    }
}